package at.fhj.ase.business;

import java.io.FileWriter;
import java.io.IOException;
import java.util.Date;
import java.util.List;

import javax.xml.stream.XMLOutputFactory;
import javax.xml.stream.XMLStreamException;
import javax.xml.stream.XMLStreamWriter;

import at.fhj.ase.data.AttackOutput;

public class OutputGenerator {
	
	public static void reportToXHTML(List<AttackOutput> attackOutput, String outputFileName ) throws XMLStreamException, IOException{
		
		XMLOutputFactory xof =  XMLOutputFactory.newInstance();
		XMLStreamWriter xtw = null;
		xtw = xof.createXMLStreamWriter(new FileWriter(outputFileName));
		xtw.writeStartDocument("utf-8","1.0");		
//		xtw.writeComment("all elements here are explicitly in the HTML namespace");
		xtw.setPrefix("html", "http://www.w3.org/1999/xhtml");
		xtw.writeStartElement("http://www.w3.org/1999/xhtml","html");
		xtw.writeNamespace("html", "http://www.w3.org/1999/xhtml");
		xtw.writeStartElement("http://www.w3.org/1999/xhtml","head");
		xtw.writeStartElement("http://www.w3.org/1999/xhtml","title");
		xtw.writeCharacters("AttackReport, created on: " + new Date().toString());
		xtw.writeEndElement();
		//for file specific CSS formats, partially taken from http://de.selfhtml.org/css/formate/zentrale.htm
		xtw.writeStartElement("http://www.w3.org/1999/xhtml", "style");
		xtw.writeAttribute("type", "text/css");
		xtw.writeCharacters(".attackEntry {color:black; border:1px outset gray; margin:.5em; padding:.5em; background-color:#efd;\n" +
							"h1{font-size:36px;}}" +
							".lineNumber,.content,.kindOfAttack,.description{color:red;}");
		xtw.writeEndElement();
		xtw.writeEndElement();
		xtw.writeStartElement("http://www.w3.org/1999/xhtml","body");
		xtw.writeStartElement("http://www.w3.org/1999/xhtml", "h1");
		xtw.writeCharacters(attackOutput.get(0)!=null ? attackOutput.get(0).getLogline().getFileName().getName():"no attacks found");
		xtw.writeEndElement();
		
		for(AttackOutput element: attackOutput){				
			
			xtw.writeStartElement("http://www.w3.org/1999/xhtml","div");
			xtw.writeAttribute("class", "attackEntry");
			xtw.writeStartElement("http://www.w3.org/1999/xhtml","p");
			xtw.writeStartElement("http://www.w3.org/1999/xhtml","ul");
			xtw.writeStartElement("http://www.w3.org/1999/xhtml","li");
			xtw.writeStartElement("http://www.w3.org/1999/xhtml", "span");
			xtw.writeAttribute("class", "lineNumber");
			xtw.writeCharacters("Line number: ");
			xtw.writeEndElement(); //</span>
			xtw.writeCharacters(Integer.toString(element.getLogline().getLineNumber()));
			xtw.writeEndElement(); //</li>
			xtw.writeStartElement("http://www.w3.org/1999/xhtml","li");
			xtw.writeStartElement("http://www.w3.org/1999/xhtml", "span");
			xtw.writeAttribute("class", "content");
			xtw.writeCharacters("Content: ");
			xtw.writeEndElement(); //</span>
			xtw.writeCharacters(element.getLogline().getContent());
			xtw.writeEndElement();
			xtw.writeStartElement("http://www.w3.org/1999/xhtml","li");
			xtw.writeStartElement("http://www.w3.org/1999/xhtml", "span");
			xtw.writeAttribute("class", "kindOfAttack");
			xtw.writeCharacters("Kind of attack: ");
			xtw.writeEndElement(); //</span>
			
			StringBuilder buffer = new StringBuilder();			
			for(String s:element.getTags()){
				buffer.append(s).append(",");
			}
			xtw.writeCharacters(buffer.substring(0, buffer.length()-1));
			xtw.writeEndElement();//</li>
			xtw.writeStartElement("http://www.w3.org/1999/xhtml","li");
			xtw.writeStartElement("http://www.w3.org/1999/xhtml", "span");
			xtw.writeAttribute("class", "description");
			xtw.writeCharacters("Description: ");
			xtw.writeEndElement();//</span>
			xtw.writeCharacters(element.getDescription());
			xtw.writeEndElement();//</li>
			xtw.writeEndElement();//</ul>			
			xtw.writeEndElement();//</p>
			xtw.writeEndElement();	//</div>		
		}
		
		xtw.writeEndElement();//</body>
		xtw.writeEndElement();//</html>
		
		xtw.writeEndDocument();
		xtw.flush();
		xtw.close();
		
	}
}
